package org.opensource.yabts.server;

import javax.servlet.http.HttpSession;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContext;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

public class SessionHelper {
	
	/**
	 * Returns the current session
	 * @return current session
	 */
	public static HttpSession getSession() {
		ServletRequestAttributes requestAttributes = (ServletRequestAttributes) RequestContextHolder.currentRequestAttributes();
		return requestAttributes.getRequest().getSession(true);		
	}
	
	/**
	 * 
	 * @param authentication
	 */
	public static void authenticateUser(Authentication authentication) {
        SecurityContext securityContext = SecurityContextHolder.getContext();
        
        securityContext.setAuthentication(authentication);
        getSession().setAttribute("SPRING_SECURITY_CONTEXT", securityContext);
	}
	
}
